// formPackage/pages/platformModule/commissionRuleList/commissionRuleList.js

import { queryMyCommissionRuleList, deleteOneCommissionRule, queryHouseAgentCompany } from "../../../../service/platform_service"

let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    dataList: [],
    houseNumber: "",
    showRulePopup: false,
    showPopupTitle: "",
    showRuleContent: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      houseNumber: options.houseNumber
    })
    this.queryHouseAgentCompany();
  },



  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }

  },


  //查询代理公司
  queryHouseAgentCompany() {
    wx.showLoading();
    queryHouseAgentCompany(this.data.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:代理公司列表是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          dataList: res.data || []
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryHouseAgentCompany", err);
    })
  },



  //新建佣金规则:
  createNewCommissionRule() {
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/createNewCommissionRule/createNewCommissionRule',
    })
  },
  //跳转到佣金规则详情:
  pushToDetailAction(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/formPackage/pages/platformModule/createNewCommissionRule/createNewCommissionRule?id=' + id,
    })
  },

  //删除一个佣金规则:
  deleteOneCommission(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: "是否删除该佣金规则？",
      confirmColor: "#FF0000",
      success: res => {
        if (res.confirm) {
          this.deleteOneCommissionRule(id, index);
        }
      }
    })
  },


  //删除佣金请求:
  deleteOneCommissionRule(id, index) {
    wx.showLoading();
    deleteOneCommissionRule(id).then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "删除成功",
          icon: "success"
        })
        let arr = this.data.dataList;
        arr.splice(index, 1);
        this.setData({
          dataList: arr
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:deleteOneCommissionRule", err);
    })
  },
  //查看佣金弹窗
  seeRuleDetailAction(e) {
    let text = e.currentTarget.dataset.text;
    let title = e.currentTarget.dataset.title;
    this.setData({
      showRuleContent: text,
      showPopupTitle: title,
      showRulePopup: true
    })
  },

  clickCenterPopupOverlay() {
    this.setData({
      showRulePopup: false
    })
  },

  //报备点击:
  companyReportAction(e) {
    let item = e.currentTarget.dataset.item;
    let data = {
      companyName: item.companyName,
      companyNumber: item.companyNumber,
      companyPhone: item.contactPhone,
      commissionName: item.name,
      commission: item.commission
    }
    app.globalData.reportCompanyData = data;
    wx.navigateBack({
      delta: 0,
    })
  },


})